<template>
  <div class="clock">
    <!-- 每日打卡 -->
    <el-row>
      <el-col :span="16">
        <div class="clockIn">
          <el-card class="clockTime" shadow="never">
            <h1>{{ currentHour }}</h1>
            <div class="buttonBox" @click="openClock" v-if="clockType!==2">
              <p v-if="clockType==0">上班打卡</p>
              <p v-if="clockType==1">下班打卡</p>
            </div>
            <div class="buttonBox successBox" v-if="clockType==2">
              <p>打卡成功</p>
            </div>
            
            <div class="time">{{ clockTime }}</div>
          </el-card>
        </div>
      </el-col>
      <el-col :span="8">
        <div
          :style="{
            width: '300px',
            border: '1px solid #d9d9d9',
            borderRadius: '4px',
          }"
        >
          <a-calendar :fullscreen="false" @select="onSelect" />
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" style="padding: 0 10px">
        <el-descriptions title="打卡信息" >
          <el-descriptions-item label="上班时间">{{
            currentHistoryData.clock_in_time||'暂无'
          }}</el-descriptions-item>
          <el-descriptions-item label="上班打卡地点">{{
            currentHistoryData.clock_in_address||'暂无'
          }}</el-descriptions-item>
           <el-descriptions-item label="打卡状态">{{
            clockTypeList[currentHistoryData.clock_status]||'暂无'
          }}</el-descriptions-item>
          <!-- <el-descriptions-item label="上班打卡备注">{{
            currentHistoryData.clock_in_remark
          }}</el-descriptions-item> -->
         
         
          <el-descriptions-item label="下班时间">{{
            currentHistoryData.clock_out_time||'暂无'
          }}</el-descriptions-item>
          <!-- <el-descriptions-item label="下班打卡备注">{{
            currentHistoryData.clock_out_time
          }}</el-descriptions-item> -->
         
           <el-descriptions-item label="下班打卡地点">
            {{ currentHistoryData.clock_out_address||'暂无' }}
          </el-descriptions-item>
          
          <el-descriptions-item label="考勤状态">{{
            attendanceTypeList[currentHistoryData.attendance_status]||'暂无'
          }}</el-descriptions-item>
        </el-descriptions>
      </el-col>
    </el-row>

    <!-- 打卡确认对话框 -->
    <el-dialog
      title="打卡信息"
      :visible.sync="clockDialogVisible"
      width="30%"
      center
    >
      <el-form :model="infoForm">
        <el-form-item label="时间" :label-width="formLabelWidth">
          <el-input
            v-model="infoForm.clock_time"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>

        <el-form-item label="打卡地址" :label-width="formLabelWidth">
          <el-input
            v-model="infoForm.clock_address"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clockDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClock">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import dayjs from "dayjs";
import { location } from "../../utils/location";
import { getHistoryClock, clock,getTodayClock,getClockType,getAttendanceTypes } from "@/api/clock";
export default {
  data() {
    return {
      username: window.sessionStorage.getItem("username")?.replace(/["]/g, ""),
      value: new Date(),
      //当前时间
      currentHour: "",
      //用户基本信息
      userInfo: [],
      //打卡对话框
      clockDialogVisible: false,
      //提交打卡信息
      infoForm: {
        clock_time: "",
        id:0,
        clock_address:'乐山市市中区乐山师范学院'
      },
      //表单宽度
      formLabelWidth: "80px",
      location: "",
      clockTime: "",
      tableData: "",
      historyData: {},
      clockType: 0,
      clockTypeList:{},
      currentHistoryData:{},
      attendanceTypeList:{}
    };
  },
  methods: {
    getHours() {
      this.currentHour = dayjs().format('HH:mm:ss')
    },
    //获取地理位置
    getLocation() {
      const that = this;
      AMap.plugin("AMap.Geolocation", function () {
        var geolocation = new AMap.Geolocation({
          // 是否使用高精度定位，默认：true
          enableHighAccuracy: false,
          // 设置定位超时时间，默认：无穷大
          timeout: 10000,
          // 定位按钮的停靠位置的偏移量
          offset: [10, 20],
          //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
          zoomToAccuracy: true,
          //  定位按钮的排放位置,  RB表示右下
          position: "RB",
        });

        geolocation.getCurrentPosition(function (status, result) {
          if (status == "complete") {
            onComplete(result);
            console.log("success");
          } else {
            geolocation.getCityInfo((status, result) => {
              //只能获取当前用户所在城市和城市的经纬度
              if (status == "complete") {
                this.surePosition(result.position[0], result.position[1]);
                console.log("获取定位");
              } else {
                that.showCity = "未获取定位";
                that.getServiceType(0);
                console.log("未获取定位");
              }
            });
          }
        });

        function onComplete(data) {
          // data是具体的定位信息
          console.log(data);
        }

        function onError(data) {
          // 定位出错
          console.log("定位出错");
        }
      });
    },
    getLngLocation(_this) {
      AMap.plugin("AMap.CitySearch", function () {
        var citySearch = new AMap.CitySearch();
        citySearch.getLocalCity(function (status, result) {
          if (status === "complete" && result.info === "OK") {
            // 查询成功，result即为当前所在城市信息
            _this.infoForm.clock_address = result.province + result.city;
          }
        });
      });
    },
    openClock() {
      this.clockDialogVisible = true;
      this.infoForm.clock_time = dayjs().format('YYYY-MM-DD hh:mm:ss')
    },
    async addClock() {
      clock(this.infoForm).then((res) => {
          this.clockType = res.clock_status;
          this.$message.success("打卡成功，祝您工作愉快！");
          this.infoForm = {
            clock_address:'乐山市市中区乐山师范学院'
          };
          this.clockDialogVisible = false;
          this.getTodayClock();
        });
    },
    onSelect(value) {
      this.currentHistoryData = this.historyData[String(dayjs(value).format("YYYY-MM-DD").slice(8))];
      if(!this.currentHistoryData){
        this.currentHistoryData={
          clock_in_time:'',
          clock_in_address:'',
          clock_status:'',
          clock_out_time:'',
          clock_out_address:'',
          attendance_status:''
        }
      }
    },
    getToMonth(){
      getHistoryClock({ month: dayjs().format("YYYY-MM") }).then(
        (res) => {
          this.historyData = res;
        }
      );
    },
    getTodayClock(){
      getTodayClock().then(res=>{
      this.clockType = res.clock_status
      this.infoForm.id = res.id;
      if(res.clock_status==0){
        this.infoForm.clock_type=1
      }else{
        this.infoForm.clock_type=2
      }
      this.currentHistoryData=res
      console.log(' this.infoForm', res)
    })
    }
  },
  created() {
    this.timer = setInterval(() => {
      this.getHours();
    }, 1000);
  },
  mounted() {
    // this.getLocation(); // 调用获取地理位置
    this.getLngLocation(this);
   this.getTodayClock();
    getClockType().then((res)=>{
      this.clockTypeList = res;
    })
    this.getToMonth()
    getAttendanceTypes().then(res=>{
      this.attendanceTypeList=res;
    })
  },
};
</script>

<style lang="less">
.clock {
  // display: flex;
  .clockIn {
    flex: 5;
    margin: 0 20px;
    .clockTime {
      // margin-top: 20px;
      // height: 500px;
      background: white;
      margin-bottom: 20px;
      h1 {
        font-size: 46px;
        text-align: center;
        color: #303133;
      }
      h2 {
        font-size: 28px;
        text-align: center;
        padding: 30px 0 10px 0;
        color: #606266;
      }
      .buttonBox {
        width: 240px;
        height: 240px;
        margin: 40px auto;
        background: linear-gradient(#6fcafa, #4064e1);
        border-radius: 50%;
        box-shadow: 0 0 8px 5px #9cc3f9;

        p {
          font-size: 50px;
          font-weight: 600;
          color: white;
          text-align: center;
          line-height: 240px;
          cursor: pointer;
        }
      }
      .successBox{
        background: linear-gradient( rgba(0, 194, 156,0.8),#00c196);
        box-shadow: 0 0 8px 5px #00c19762;
      }
      .time {
        display: flex;
        justify-content: center;
        font-size: 20px;
      }
    }
  }
}
.calendar {
  flex: 2;
  height: 400px;
  margin-right: 10px;
  .el-calendar-table .el-calendar-day {
    width: 50px;
    height: 40px;
  }
  .el-calendar-table tr td:first-child {
    border-left: 0;
  }
  .el-calendar-table td {
    border-bottom: 0;
    border-right: 0;
  }
  .calendar .el-calendar-table td {
    border-bottom: 0;
    border-right: 0;
    text-align: center !important;
    line-height: 24px;
  }
}
.el-descriptions__body {
  padding: 20px;
}
</style>
